/**
 * Developer : aOmMiez
 * Date : 4/9/13 AD
 * Time : 12:28 PM
 * File : friends.js
 */


var Friends = function(){};

Friends.prototype.init = function(req, res){
	if(req.method == 'GET') {
		this.Friends(req, res);
	}
	else if(req.method == 'POST') {
        this.Friends(req, res);
	}
	else if(req.method == 'PUT') {
		// put method
	}
	else if(req.method == 'DELETE') {
		this.delete(req, res);
	}
};

Friends.prototype.Friends = function(req, res){
	var uid = req.param("uid");
	var sql = "SELECT id,userId1,userId2,areFriend FROM Friend WHERE (userId1="+Mysql.escape(uid)+" OR userId2="+Mysql.escape(uid)+") AND areFriend='2'";
    GLOBAL.Mysql.query(sql, function(error, results){
        if(error){
            throw new Exception(9999, 'Mysql Error: '+err, function(){
                console.log('Mysql Error: '+err);
                res.send(this.toString());
                res.end();
            });
    	} else {
            var friends = results;
            var length = results.length;
            var data = { data: friends, length: length };
            res.send(JSON.stringify(data));
            res.end();
            console.log(results.length);
        }
    });
};

Friends.prototype.delete = function(req, res){
	var userId1 = req.body.userId1;
	var userId2 = req.body.userId2;
	var deleteService = new (Services('deleteFriend'))(userId1, userId2, {
		success: function(data){
			res.send(JSON.stringify({ success: true }));
			res.end();
		},
		error: function(code, message){
			throw new Exception(code, message, function(){
				res.send(this.toString());
				res.end();
			});
		}
	});
};
module.exports = Friends;